<template>
  <div id="app">
    <transition :name="pageTransition">
      <router-view class="router-view" />
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageTransition: "slide-left",
    };
  },
  watch: {
    // 监听路由跳转的级别，设置对应的动画
    $route(newVal, oldVal) {
      if (newVal.meta.index < oldVal.meta.index) {
        this.pageTransition = "slide-right";
      } else if (newVal.meta.index > oldVal.meta.index) {
        this.pageTransition = "slide-left";
      } else {
        this.pageTransition = "";
      }
    },
  },
};
</script>

<style lang="scss">
.router-view {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: 0 auto;
  -webkit-overflow-scrolling: touch;
}

/*切换动画*/
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  height: 100%;
  will-change: transform;
  transition: all 500ms;
  position: absolute;
  backface-visibility: hidden;
}

.slide-right-enter {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}
</style>
